﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Xml;

namespace 行政区域
{
    public partial class Form2 : Form
    {
        private delegate void setProgCallback(int value, int total);
        private delegate void GetTilePolyCallback(string name);
        private delegate void GetPolyBianJieCallBack(string type);
        private delegate void SetTextCallback(string text, string type);
        private int num = 0;
        private ArrayList pNameList;
        private System.Threading.Timer tm;
        private System.Threading.Timer tm2;

        public Form2()
        {
            InitializeComponent();

            this.webBrowser1.Navigate(new Uri(Application.StartupPath + "\\map.htm"));
            try
            {
                ShowXmlTreeNode();
            }
            catch (Exception)
            {

            }
        }
        private void ShowXmlTreeNode()
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load("Xzqy.XML");

            XmlNodeList xmlNodes = xmlDoc.DocumentElement.ChildNodes;

            this.treeView1.BeginUpdate();
            this.treeView1.Nodes.Clear();
            XmlNode2TreeNode(xmlNodes, this.treeView1.Nodes);
            this.treeView1.EndUpdate();
        }
        private void XmlNode2TreeNode(XmlNodeList xmlNode, TreeNodeCollection treeNode)
        {
            foreach (XmlNode var in xmlNode)
            {
                if (var.NodeType != XmlNodeType.Element)
                {
                    continue;
                }
                TreeNode newTreeNode = new TreeNode();
                newTreeNode.Text = var.Attributes["Name"].Value;
                newTreeNode.Tag = var.Attributes["url"].Value;
                newTreeNode.ToolTipText = var.Attributes["HelperCode"].Value;
                if (var.HasChildNodes)
                {
                    if (var.ChildNodes[0].NodeType == XmlNodeType.CDATA)
                    {
                        newTreeNode.Tag = var.ChildNodes[0].Value;
                    }
                    XmlNode2TreeNode(var.ChildNodes, newTreeNode.Nodes);
                }
                treeNode.Add(newTreeNode);
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            DataSet ds = new DataSet();
            ds.ReadXml("Xzqy.xml");
            DataTable tb1 = ds.Tables[0];
            DataTable tb2 = tb1.Copy();
            tb2.Columns.Add("x");
            tb2.Columns.Add("y");

            TimerCallback tcb = new TimerCallback(this.execute);
            TimerCallback tcb2 = new TimerCallback(this.execute2);

            

            foreach (DataRow row in tb2.Rows)
            {
                Thread.Sleep(15000);
                string name = row["Name"].ToString();

                this.tm = new System.Threading.Timer(tcb, "", 1000, 15000);
                this.tm2 = new System.Threading.Timer(tcb2, "", 15000, 15000);
            }
        }
        private void execute(object obj)
        {
            string ep = obj.ToString();
            if (ep != null)
            {
                this.GetTilePoly(this.pNameList[this.num].ToString());
                this.num++;
                if (this.num == this.pNameList.Count)
                {
                    this.tm.Change(0, -1);
                    this.tm.Dispose();
                }
            }
        }
        private void execute2(object obj)
        {
            string ep = obj.ToString();
            if (ep != null)
            {
                this.GetPolyBianJie(ep);
                if (this.num == this.pNameList.Count)
                {
                    this.tm2.Change(0, -1);
                    this.tm2.Dispose();
                    MessageBox.Show("game over!!!");
                }
            }
        }
        private void GetTilePoly(string name)
        {
            if (this.webBrowser1.InvokeRequired)
            {
                GetTilePolyCallback d = new GetTilePolyCallback(this.GetTilePoly);
                base.Invoke(d, new object[]
				{
					name
				});
            }
            else
            {
                this.webBrowser1.Document.GetElementById("districtName").SetAttribute("value", name);
                this.webBrowser1.Document.GetElementById("button1").InvokeMember("click");
            }
        }
        private void GetPolyBianJie(string type)
        {
            if (this.webBrowser1.InvokeRequired)
            {
               GetPolyBianJieCallBack d = new GetPolyBianJieCallBack(this.GetPolyBianJie);
                base.Invoke(d, new object[]
				{
					type
				});
            }
            else
            {
                this.SetText(this.webBrowser1.Document.GetElementById("Div1").InnerText, type);
            }
        }
        private void SetText(string text, string type)
        {
            if (this.textBox1.InvokeRequired)
            {
                SetTextCallback d = new SetTextCallback(this.SetText);
                base.Invoke(d, new object[]
				{
					text
				});
            }
            else
            {
                this.textBox1.Text = text;
                //this.GenFile(type, text);
            }
        }
    }
}
